package com.px.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.biz.dto.SalesContractDTO;
import com.px.biz.service.SalesDetailService;
import com.px.biz.service.SalesOrderService;
import com.px.common.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.px.biz.dao.SalesContractDao;
import com.px.biz.domain.SalesContractDO;
import com.px.biz.service.SalesContractService;



@Service
public class SalesContractServiceImpl extends ServiceImpl<SalesContractDao, SalesContractDO> implements SalesContractService {
	@Autowired
	private SalesContractDao salesContractDao;
	@Autowired
	private SalesDetailService salesDetailService;
	@Autowired
	private SalesOrderService salesOrderService;
	@Autowired
	private SalesContractService salesContractService;
	
	@Override
	public SalesContractDO get(String id){
		return salesContractDao.get(id);
	}
	
	@Override
	public List<SalesContractDO> list(Map<String, Object> map){
		return salesContractDao.list(map);
	}

	@Override
	public R saveCont(SalesContractDTO salesContractDTO) {
		//合同
		SalesContractDO salesContract=new SalesContractDO();
		BeanUtils.copyProperties(salesContractDTO,salesContract);
		if(StringUtils.isBlank(salesContract.getId())){
			salesContract.setId(IdUtil.getStrId());
		}
		//订单
//		SalesOrderDO salesOrder=new SalesOrderDO();
//		if(StringUtils.isBlank(salesContractDTO.getOrderId())){
//			salesOrder.setId(IdUtil.getStrId());
//		}else {
//			salesOrder.setId(salesContractDTO.getOrderId());
//		}
//		salesOrder.setOrderNo(salesContractDTO.getOrderNo());
//		salesOrder.setContractId(salesContract.getId());
//		salesOrder.setCreateUser(ShiroUtils.getUserId());
//		salesOrder.setCreateTime(new Date());
//		//订单详情
//		BigDecimal allAmount = new BigDecimal(0);
//		List<SalesDetailDO> salesDetailList=salesContractDTO.getDetails();
//		if(null!=salesDetailList&&salesDetailList.size()>0){
//			for (SalesDetailDO salesDetail : salesDetailList) {
//				if (salesDetail.getCpId() == null) {
//					throw new BDException("订单详情存在未选择产品");
//				}
//				if (salesDetail.getUnitPrice() == null) {
//					throw new BDException("订单详情存在产品未设置单价");
//				}
//				if (salesDetail.getNum() == null) {
//					throw new BDException("订单详情存在产品未设置数量");
//				}
//				salesDetail.setId(IdUtil.getStrId());
//				salesDetail.setOrderId(salesOrder.getId());
//				BigDecimal totalPrice = NumberUtil.mul(salesDetail.getUnitPrice(), salesDetail.getNum());
//				salesDetail.setTotalPrice(totalPrice);
//				allAmount = NumberUtil.add(allAmount, totalPrice);
//			}
//		}else {
//			throw new BDException("请选择产品");
//		}
//		salesDetailService.remove(new LambdaQueryWrapper<SalesDetailDO>().eq(SalesDetailDO::getOrderId,salesOrder.getId()));
//		salesDetailService.saveOrUpdateBatch(salesDetailList);
//		salesOrder.setTotalPrice(allAmount);
//		salesOrderService.saveOrUpdate(salesOrder);
//		salesContract.setOrderId(salesOrder.getId());
//		salesContract.setSalesAmount(allAmount);
		salesContract.setCreateUser(ShiroUtils.getUserId());
		salesContract.setUpdateUser(ShiroUtils.getUserId());
		salesContract.setCreateTime(new Date());
		salesContract.setUpdateTime(new Date());
		salesContractService.saveOrUpdate(salesContract);
		return R.ok();
	}

	@Override
	public SalesContractDTO getDtoById(String id) {
		return salesContractDao.getDtoById(id);
	}

   /* @Override
    public List<SalesContractDO> getYujing() {
        return salesContractDao.getYujing();
    }*/

	@Override
	public List<HashMap> getYujing() {
		return salesContractDao.getYujing();
	}


}
